<template>
    <el-dialog :title="title" custom-class="el-dialog-600" v-model="visible">
        <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
            <el-form-item label="分类名" prop="title">
                <el-col :span="12">
                    <el-input clearable v-model="form.title" placeholder="请输入分类名" />
                </el-col>
            </el-form-item>

            <el-form-item label="描述" prop="desc">
                <el-col :span="24">
                    <el-input type="textarea" v-model="form.desc" />
                </el-col>
            </el-form-item>

            <el-form-item label="是否推荐" prop="support">
                <el-radio-group v-model="form.support">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible = false"> 取 消 </el-button>
            <el-button type="primary" :loading="loading" @click="onSubmit"> 确 认 </el-button>
        </template>
    </el-dialog>
</template>

<script>
import { getCurrentInstance, ref, reactive, watch } from 'vue';
import { adminClassify as Api } from 'api';

export default {
    setup() {
        const { proxy } = getCurrentInstance();

        let id = ref(null);
        let loading = ref(false); //等待按钮
        let visible = ref(false); //弹窗开关
        let title = ref(null); // 标题
        // 表单
        let form = reactive({
            title: '',
            desc: '',
            support: 0
        });
        // 验证
        let rules = {
            title: [{ required: true, message: '请输入分类名', trigger: 'change' }]
        };

        // 初始化
        const init = () => {
            title.value = id.value ? '编辑' : '新增';
            if (id.value) getById();
        };
        // 获取详情
        const getById = async () => {
            const { code, data } = await proxy.$http.get(Api.getById, {
                params: { id: id.value }
            });

            if (code === 200) {
                form.title = data.title;
                form.desc = data.desc;
                form.support = Number(data.support);
            }
        };
        // 提交表单
        const onSubmit = async () => {
            await proxy.$refs['formRef'].validate((valid) => {
                if (valid) loading.value = true;
            });

            if (loading.value) {
                let url = id.value ? Api.update : Api.add;
                let params = {
                    title: form.title,
                    desc: form.desc,
                    support: form.support
                };

                if (id.value) params.id = id.value;

                const { code, msg, data } = await proxy.$http.post(url, params);

                if (code === 200) {
                    proxy.$msg.success(msg);
                    proxy.$parent.initTableSelectionIds();
                    visible.value = false;
                } else {
                    proxy.$msg.error(msg ? msg : data);
                }

                loading.value = false;
            }
        };

        watch(visible, (newVal, oldVal) => {
            newVal ? init() : proxy.$refs['formRef'].resetFields();
        });

        return {
            id,
            loading,
            visible,
            title,
            form,
            rules,
            onSubmit
        };
    }
};
</script>